<template>
  <div class="flexContent">
    <div class="searchBox">
      <el-form ref="formRef" class="demo-form-inline" @submit.native.prevent label-position="left" label-width="auto">
        <div class="serFlexBox">
          <div class="inputBox">
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="暂无搜索">
                  <el-input style="width: 70%;" />
                </el-form-item>
              </el-col>

            </el-row>
          </div>
          <div class="btnBox">
            <el-button class="searchBtn" icon="el-icon-search">搜索</el-button>
            <el-button class="resetBtn" icon="el-icon-refresh-right">重置</el-button>
          </div>
        </div>
      </el-form>
    </div>
    <div class="add">
      <el-button class="addBtn" @click="addFn">新增</el-button>
    </div>
    <div class="table" id="tableBox" v-height>
      <div style="flex:1;">
        <el-table class="tablebox" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
          :data="menuList" style="width: 100%" height="100%" size="medium">

          <el-table-column align="center" label="菜单名称">
            <template v-slot="{ row }">
              <el-tag type="primary" v-if="row.level == '1'">{{ row.menuName }}</el-tag>
              <el-tag type="success" v-else-if="row.level == '2'">{{ row.menuName }}</el-tag>
              <el-tag type="warning" v-else>{{ row.menuName }}</el-tag>
              <!-- <span>{{ row.menuName }}</span> -->
            </template>
          </el-table-column>
          <el-table-column prop="url" align="center" label="URL路径" />
          <el-table-column prop="level" align="center" label="菜单级别">
            <template v-slot="{ row }">
              <!-- <span>{{ row | levelName }}</span> -->
              <el-tag type="primary" v-if="row.level == '1'">主菜单</el-tag>
              <el-tag type="success" v-else-if="row.level == '2'">子菜单</el-tag>
              <el-tag type="warning" v-else>{{ row.url }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="sort" align="center" label="排序" />
          <el-table-column prop="updateName" align="center" label="更新人" />
          <el-table-column prop="updateTime" align="center" label="更新时间">
            <template v-slot="{ row }">
              {{ row.updateTime | parseTime }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template v-slot="{ row }">
              <el-button type="text" size="big" style="margin-right: 10px; color: rgb(0, 132, 255)"
                @click.stop="edit(row)">编辑</el-button>
              <el-button v-if="row.level === '1' || row.level === '2'" slot="reference" style="color: rgb(132, 0, 255)"
                type="text" size="big" @click.stop="addFn(row)">添加下级</el-button>
              <el-button type="text" size="big" style="margin-right: 10px; color: red"
                @click="$delFn('菜单', del, row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="paginationBox">
        <el-pagination :current-page="page.pageNum" background :page-sizes="$pageSizes" :page-size="page.pageSize"
          layout="total,->, sizes, prev, pager, next, jumper" :total="page.total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </div>
    <AddDialog :close-on-click-modal="false" :show-dialog.sync="showDialog" :width="width" :title="title" @btnOk="btnOk"
      @close="close">
      <el-form ref="addFormRef" :disabled="title === '详情'" :model="addForm" :rules="addRules" label-width="100px">
        <el-form-item label="菜单名称" style="width: 100%" prop="menuName">
          <el-input v-model="addForm.menuName" />
        </el-form-item>
        <el-form-item label="菜单级别" style="width: 100%" prop="level">
          <el-input v-model="addForm.level" disabled />
        </el-form-item>
        <el-form-item label="URL路径" style="width: 100%" prop="url">
          <el-input v-model="addForm.url" />
        </el-form-item>
        <el-form-item label="排序" style="width: 100%" prop="sort">
          <el-input v-model="addForm.sort" />
        </el-form-item>
      </el-form>
    </AddDialog>
  </div>
</template>

<script>
import { parseTime2 } from "@/utils";
import AddDialog from "@/components/Goods/AddDialog";
import { getMenuInfo, addMenu, updateMenuDetail, delMenu } from "@/api/system";
export default {
  name: "MenuInfo",
  components: {
    AddDialog,
  },
  filters: {
    parseTime(val) {
      return parseTime2(val);
    },
  },
  data() {
    return {
      loading: false,
      width: "50%",
      title: "新增",
      parentMenuId: null,
      showDialog: false,
      levelNmae: "",
      addForm: {
        menuName: "", // 菜单名称
        level: "1", // 菜单级别
        url: "", // 资源路径
        sort: "", //	排序
      },
      addRules: {
        menuName: [
          {
            required: true,
            message: "请输入菜单名称",
            trigger: "change",
          },
          {
            min: 1,
            max: 10,
            message: "长度在1到10个字符",
            trigger: "change",
          },
          {
            pattern: /^([\u4e00-\u9fa5A-Za-z])+$/,
            message: "只能输入中英文",
            trigger: "change",
          },
        ],
        level: [
          {
            required: true,
            message: "请输入菜单级别",
            trigger: "change",
          },
        ],
        url: [
          {
            min: 1,
            max: 30,
            message: "长度在1到30个字符",
            trigger: "change",
          },
          {
            required: true,
            message: "请输入资源路径",
            trigger: "change",
          },
        ],
        bySystem: [
          {
            required: true,
            message: "请选择菜单类型",
            trigger: "change",
          },
        ],
        sort: [
          {
            required: true,
            message: "请输入排序",
            trigger: "change",
          },
          {
            pattern: /^([0-9])+$/,
            message: "只能输入中英文",
            trigger: "change",
          },
        ],
      },
      menuList: [],
      mennId: "0",
      date: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      },
      posId: "",
    };
  },
  created() {
    this.getMenuInfo();
  },
  methods: {
    // 获取菜单列表
    async getMenuInfo() {
      const res = await getMenuInfo({ ...this.page });
      if (res.code === 200) {
        console.log(res);
        const result = res.data;
        this.menuList = result.list;
        console.log(result, "data");
        // this.page.pageSize = result.pageSize
        this.page.pages = result.pages;
        this.page.pageNum = result.pageNum;
        this.page.total = result.total;
        console.log("menuList", this.menuList);
      } else {
        // this.$message.error(res.message);
      }
    },
    addFn(row) {
      this.title = "新增";
      this.showDialog = true;
      if (row.level === "1") {
        this.addForm.level = "2";
        this.parentMenuId = row.id;
      } else if (row.level === "2") {
        this.addForm.level = "3";
        this.parentMenuId = row.id;
      } else {
        this.addForm.level = "1";
        this.parentMenuId = null;
      }
    },
    // 获取条数改变
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNum = 1;
      console.log(`每页 ${val} 条`);
      this.getMenuInfo();
    },
    // 获取页数改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page.pageNum = val;
      this.getMenuInfo();
    },
    close() {
      this.showDialog = false;
      this.addForm = {
        menuName: "", // 菜单名称
        level: "1", // 菜单级别
        url: "", // 资源路径
        sort: "", //	排序
      };
      this.$refs.addFormRef.resetFields();
    },
    btnOk() {
      // 判断是新增还是修改
      if (this.title === "新增") {
        // 新增
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            //
            const res = await addMenu({
              ...this.addForm,
              parentMenuId: this.parentMenuId,
            });
            if (res.code === 200) {
              this.$message.success("新增成功");
              this.getMenuInfo();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      } else if (this.title === "详情") {
        // 关闭
        this.close();
      } else if (this.title === "编辑") {
        // 修改
        this.$refs.addFormRef.validate(async (isOk) => {
          if (isOk) {
            const res = await updateMenuDetail({
              ...this.addForm,
              id: this.posId,
            });
            if (res.code === 200) {
              this.$message.success("修改成功");
              this.getMenuInfo();
              this.close();
            } else {
              // this.$message.error(res.message);
            }
          } else {
            this.$message.warning("请检查输入信息");
          }
        });
      }
    },
    async getDetail(row) {
      this.showDialog = true;
      this.title = "详情";
      console.log(row);
      this.addForm = row;
    },
    async edit(row) {
      this.showDialog = true;
      this.title = "编辑";
      console.log(row);
      this.addForm.menuName = row.menuName;
      this.addForm.level = row.level;
      this.addForm.sort = row.sort;
      this.addForm.url = row.url;
      this.posId = row.id;
    },
    async del(row) {
      const res = await delMenu({ id: row.id });
      if (res.code === 200) {
        this.$message.success("删除成功");
        if (this.menuList.length === 1 && this.page.pageNum > 1)
          this.page.pageNum--;
        this.getMenuInfo();
      } else {
        // this.$message.error(res.message);
      }
    },
    selByTypeFn(val) {
      this.mennId = val;
      this.page = {
        pageNum: 1,
        pageSize: 20,
        total: 0,
      };
      this.getMenuInfo();
    },
  },
};
</script>

<style lang="scss" scoped></style>
